package com.codedancer.campus.manager.factory;


import static com.codedancer.campus.enums.SystemMessageType.CIRCLE_EXAMINE;
import static com.codedancer.campus.enums.SystemMessageType.COMMODITY_EXAMINE;
import static com.codedancer.campus.enums.SystemMessageType.COMMODITY_OFF;
import static com.codedancer.campus.enums.SystemMessageType.COMMODITY_PUBLISH;
import static com.codedancer.campus.enums.SystemMessageType.DYNAMIC_EXAMINE;
import static com.codedancer.campus.enums.SystemMessageType.FOLLOW_USER;
import static com.codedancer.campus.enums.SystemMessageType.LOST_FOUND_ITEM_EXAMINE;
import static com.codedancer.campus.enums.SystemMessageType.LOST_FOUND_ITEM_PUBLISH;

import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.enums.SystemMessageType;
import com.codedancer.campus.manager.handler.SystemMessageHandler;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import static com.codedancer.campus.enums.SystemMessageType.*;

@Component
public class SystemMessageFactory implements InitializingBean, ApplicationContextAware {

    private ApplicationContext applicationContext;
    private static final Map<SystemMessageType, SystemMessageHandler> SYSTEM_MESSAGE_HANDLER_MAP = new HashMap<>();

    /**
     * 根据系统消息类型获取对应的处理器
     *
     * @param messageType
     * @return
     */
    public SystemMessageHandler getSystemMessageHandler(Integer messageType) {
        if (Objects.equals(COMMODITY_EXAMINE.getValue(), messageType)) {
            // 商品审核
            return SYSTEM_MESSAGE_HANDLER_MAP.get(COMMODITY_EXAMINE);
        } else if (Objects.equals(COMMODITY_OFF.getValue(), messageType)) {
            // 商品下架
            return SYSTEM_MESSAGE_HANDLER_MAP.get(COMMODITY_OFF);
        } else if (Objects.equals(COMMODITY_PUBLISH.getValue(), messageType)) {
            // 商品发布
            return SYSTEM_MESSAGE_HANDLER_MAP.get(COMMODITY_PUBLISH);
        } else if (Objects.equals(LOST_FOUND_ITEM_EXAMINE.getValue(), messageType)) {
            // 失物招领审核
            return SYSTEM_MESSAGE_HANDLER_MAP.get(LOST_FOUND_ITEM_EXAMINE);
        } else if (Objects.equals(LOST_FOUND_ITEM_PUBLISH.getValue(), messageType)) {
            // 失物招领发布
            return SYSTEM_MESSAGE_HANDLER_MAP.get(LOST_FOUND_ITEM_PUBLISH);
        } else if (Objects.equals(CIRCLE_EXAMINE.getValue(), messageType)) {
            // 圈子审核
            return SYSTEM_MESSAGE_HANDLER_MAP.get(CIRCLE_EXAMINE);
        } else if (Objects.equals(DYNAMIC_EXAMINE.getValue(), messageType)) {
            // 动态审核
            return SYSTEM_MESSAGE_HANDLER_MAP.get(DYNAMIC_EXAMINE);
        } else if (Objects.equals(CONFESSIONAL_WALL_EXAMINE.getValue(), messageType)) {
            // 表白墙审核
            return SYSTEM_MESSAGE_HANDLER_MAP.get(CONFESSIONAL_WALL_EXAMINE);
        } else if (Objects.equals(MESSAGE_EMPTY.getValue(), messageType)) {
            // 信息清空
            return SYSTEM_MESSAGE_HANDLER_MAP.get(MESSAGE_EMPTY);
        } else if (Objects.equals(FOLLOW_USER.getValue(), messageType)) {
            // 用户关注
            return SYSTEM_MESSAGE_HANDLER_MAP.get(FOLLOW_USER);
        } else {
            throw new BusinessException("系统消息类型不正确!");
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        applicationContext.getBeansOfType(SystemMessageHandler.class).values().forEach(bean -> {
            SYSTEM_MESSAGE_HANDLER_MAP.put(bean.getSystemMessageType(), bean);
        });
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
